package com.example.smartcity.activity;

import static java.security.AccessController.getContext;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.smartcity.R;
import com.example.smartcity.bean.BannerBean;
import com.example.smartcity.bean.BannerData;
import com.example.smartcity.bean.HotBarbageBean;
import com.example.smartcity.bean.HotGarbageBean;
import com.example.smartcity.bean.HotGarbageItem;
import com.example.smartcity.bean.Result;
import com.example.smartcity.fragment.HomeFragment;
import com.example.smartcity.utils.APIConfig;
import com.google.gson.Gson;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CheckGarbageActivity extends AppCompatActivity {
    private Banner banner;
    private EditText ed_Search;
    private ListView lv_search;

    public View view;
    private List<String> images;
    private List<Integer> img;
    private List<String> titles;
    private String result;
    private HotBarbageBean hotGarbageBean;
    private HotGarbageAdapter hotGarbageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_garbage);
        banner = findViewById(R.id.banner);
        ed_Search = findViewById(R.id.ed_search);
        lv_search = findViewById(R.id.lv_search);
        initBanner();
        initData();
        lv_search.setAdapter(hotGarbageAdapter);
        ed_Search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    SearchGarbage();
                    // 在这里执行搜索操作
                    return true;
                }
                return false;
            }
        });

    }

    public void initBanner() {
        // 网络加载图片
        List<Integer> img = new ArrayList<>();
        img.add(R.drawable.recycle1);
        img.add(R.drawable.recycle2);
        img.add(R.drawable.recycle3);
        img.add(R.drawable.recycle4);
        banner.setAdapter(new BannerImageAdapter<Integer>(img) {
                    @Override
                    public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                        // BannerImageHolder 图片加载自己实现

                        try{
                            Glide.with(getApplicationContext())
                                    .load(img.get(position))
                                    .into(holder.imageView);
                        }catch (Exception e){
                            e.printStackTrace();
                            Glide.with(getApplicationContext())
                                    .load(R.drawable.nopicture)
                                    .into(holder.imageView);
                        }
                    }
                }).addBannerLifecycleObserver(this) // 添加生命周期观察者
                .setIndicator(new CircleIndicator(this));
    }
    public void initData(){
        OkHttpClient client = new OkHttpClient();
        Request request  = new Request.Builder()
                .url("https://apis.tianapi.com/hotlajifenlei/index?key=5ce9c93a896acbb81c8dfea083343944")
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    result = response.body().string();
                    Log.i("getresult",result);
                    Gson gson = new Gson();
                    hotGarbageBean = gson.fromJson(result,HotBarbageBean.class);

                    //rowsBeans = gson.fromJson(newsBean.getRows(), listType);
                }else{
                    result = "response failure";
                    }
                hotGarbageAdapter = new HotGarbageAdapter();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 在这里执行UI操作，如设置适配器
                        // 设置适配器
                        lv_search.setAdapter(hotGarbageAdapter);
                    }
                });

            }
        });

    }
    public void SearchGarbage(){
        Log.i("KEYRUBBISH",ed_Search.getText().toString());
        OkHttpClient client = new OkHttpClient();
        Request request  = new Request.Builder()
                .url("https://apis.tianapi.com/lajifenlei/index?key=5ce9c93a896acbb81c8dfea083343944&word="+ed_Search.getText().toString())
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    result = response.body().string();
                    Log.i("getresult",result);
                    Gson gson = new Gson();
                    hotGarbageBean = gson.fromJson(result,HotBarbageBean.class);

                    //rowsBeans = gson.fromJson(newsBean.getRows(), listType);
                }else{
                    result = "response failure";
                }
                hotGarbageAdapter = new HotGarbageAdapter();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 在这里执行UI操作，如设置适配器
                        // 设置适配器
                        lv_search.setAdapter(hotGarbageAdapter);
                    }
                });

            }
        });
    }
    public class HotGarbageAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return hotGarbageBean!=null ? hotGarbageBean.getResult().getList().size() : 0;
        }
        @Override
        public Object getItem(int i) {
            return hotGarbageBean.getResult().getList().get(i);
        }
        @Override
        public long getItemId(int i) {
            return i;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            CheckGarbageActivity.HotGarbageAdapter.ViewHolder holder;
            if (view==null) {
                view = View.inflate(getApplicationContext(), R.layout.garbage_item,null);
                holder = new ViewHolder();
                holder.gbg_title = view.findViewById(R.id.garbage_name);
                holder.gbg_img = view.findViewById(R.id.garbage_icon);
                view.setTag(holder);
            } else {
                holder = (CheckGarbageActivity.HotGarbageAdapter.ViewHolder) view.getTag();
            }
            Log.i("gbgn",hotGarbageBean.getResult().getList().get(i).getName());
            Log.i("gbgt",hotGarbageBean.getResult().getList().get(i).getType()+"");
            holder.gbg_title.setText(hotGarbageBean.getResult().getList().get(i).getName());
            if(hotGarbageBean.getResult().getList().get(i).getType()==0){
                Glide.with(getApplicationContext()).load(R.drawable.recyclable_waste).override(150,150).into(holder.gbg_img);
            }else if(hotGarbageBean.getResult().getList().get(i).getType()==1){
                Glide.with(getApplicationContext()).load(R.drawable.hazardous_waste).override(150,150).into(holder.gbg_img);
            }else if(hotGarbageBean.getResult().getList().get(i).getType()==2){
                Glide.with(getApplicationContext()).load(R.drawable.wet_waste).override(150,150).into(holder.gbg_img);
            }else{
                Glide.with(getApplicationContext()).load(R.drawable.other_waste).override(150,150).into(holder.gbg_img);
            }

            return view;
        }
        class ViewHolder{
            TextView gbg_title;
            ImageView gbg_img;

        }
    }
}